// pages/my/travel/travel.js
const app = getApp();
const db = wx.cloud.database();
const myTravel = db.collection("myTravel");
const suffererTravel = db.collection("suffererTravel");

Page({

  /**
   * 页面的初始数据
   * page分页
   */
  data: {
    isAll:false,
    page:0,
    travel:[],
    sufferer:{},
    suffererDict:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.getTravel =true;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (typeof that.getTabBar === 'function' &&
      that.getTabBar()) {
      that.getTabBar().setData({
        pageCur: 'myTravel',
      })
    }
    if (app.globalData.getTravel) {
      that.setData({
        isAll: false,
        page: 0,
        travel: [],
      });
      wx.cloud.callFunction({
        name: 'getMyTravel',
        data: {
          page: that.data["page"],
        },
        success: function (res) {
          that.setData({
            travel: that.data["travel"].concat(res.result.data)
          });
          if (res.result.data.length == 100) {
            that.setData({
              page: that.data["page"] + 100
            });
          } else {
            that.setData({
            isAll: true
            });
            app.globalData.getTravel = false;
          }
        },
        fail: err => {
          console.log(err);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data["isAll"] == false) {
      wx.cloud.callFunction({
        name: 'getMyTravel',
        data: {
          page: that.data["page"],
        },
        success: function (res) {
          that.setData({
            travel: that.data["travel"].concat(res.result.data)
          });
          if (res.result.data.length == 100) {
            that.setData({
              page: that.data["page"] + 100
            });
          } else {
            that.setData({
              isAll: true
            });
            app.globalData.getTravel = false;
          }

        },
        fail: err => {
          console.log(err);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:"疫轨迹，了解感染者出行活动，发现自身感染风险",
      imageUrl:"/images/share.png",
      path:"/pages/sufferer/travel/travel",
    }
  },
  copyText: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              icon: 'none',
              title: '复制成功,请到浏览器查看信息源'
            })
          }
        })
      }
    })
  },

  showModal(e) {
    var that =this;
    suffererId = e.currentTarget.dataset.suffererid;
    if(suffererId in that.data.suffererDict){
      that.setData({
        sufferer: that.data.suffererDict[suffererId],
        modal: true
      });}
    else{
      suffererTravel.doc(suffererId).get({
        success: function (res) {
          var s= that.data.suffererDict;
          s[res.data["_id"]] = res.data;
          that.setData({
            sufferer:res.data,
            suffererDict:s,
            modal: true
          });
        },
        fail: console.error
        }
      );
    }  
  },

  hideModal(e) {
    var that = this;
    that.setData({
      modal: false
    })
  },

  delMyTravel: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.travelid;
    wx.showModal({
      title: '确认删除这条数据 ？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          myTravel.doc(id).remove({
            success: function (res) {
              wx.showToast({
                title: '删除成功',
                icon: 'none',
                duration: 1500
              });
              that.getMyTravel();
            },
          });
        }
      }
    });
  },
  
  getMyTravel: function () {
    var that = this;
    that.setData({
      isAll: false,
      page: 0,
      travel: [],
    });
    wx.cloud.callFunction({
      name: 'getMyTravel',
      data: {
        page: that.data["page"],
      },
      success: function (res) {
        that.setData({
          travel: that.data["travel"].concat(res.result.data)
        });
        if (res.result.data.length == 100) {
          that.setData({
            page: that.data["page"] + 100
          });
        } else {
          that.setData({
            isAll: true
          });
          app.globalData.getTravel = false;
        }
      },
      fail: err => {
        console.log(err);
      }
    });
  },



})